package com.u.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.u.eventInfo.Event;
import com.u.tool.MysqlUtil;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class UpdateEventStatus
 */
@WebServlet("/UpdateEventStatus")
public class UpdateEventStatus extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateEventStatus() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		String method = request.getParameter("method");
		String event_id = request.getParameter("event_id");
		
		String sql = "select * from y_r.event where event_id='"+event_id+"'";
		System.out.println(sql);
		 MysqlUtil db = new MysqlUtil();
		 db.connection();
		 List<Event> list =  db.query(Event.class, sql);
		 Event event = list.get(0);
		 if(event.getStatus().equals("uploaded")) {
			 event.setStatus("received");
		 }
		 switch(method) {
		 case("repair"):{
			 event.setStatus("repairing");
			 break;
		 }
		 case("complete"):{
			 event.setStatus("completed");
			 break;
		 }
		 case("contact"):{
			 event.setStatus("contact");
		 }
		 case("check"):{
			 break;
		 }
		 default:break;
		 }
		 db.connection();
		 db.update(event);
		 PrintWriter out = response.getWriter();
		 JSONArray json = new JSONArray();
		 json.add("success");
		 out.println(json.toString());
		 out.close();
	}

}
